package com.googlecode.yonggemovie.utils.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 底层数据库初始化、连接、关闭等操作
 * @author Kobe Su,2009-12-4
 */
public class Database {

	private static final Log log = LogFactory.getLog(Database.class);
	
	/**
	 * 数据库驱动
	 */
	private static String driverClass;

	/**
	 * 数据库访问URL
	 */
	private static String url;
	
	/**
	 * 数据库访问用户名
	 */
	private static String username;

	/**
	 * 数据库访问口令
	 */
	private static String password;

	/**
	 * 数据库配置文件
	 */
	private static final String DB_CONFIG_FILE = "com.googlecode.yonggemovie.utils.db.database";

	private static ThreadLocal<Connection> connection = new ThreadLocal<Connection>();

	private static final String CONN_DRIVER_CLASS = "connection.driver_class";
	private static final String CONN_URL = "connection.url";
	private static final String CONN_USERNAME = "connection.username";
	private static final String CONN_PASSWORD = "connection.password";
	
	//防止实例化
	private Database() {
		
	}
	
	static {
		initConfig();
	}

	//读取数据库配置文件
	private static void initConfig() {

		PropertyResourceBundle resourceBundle = (PropertyResourceBundle) PropertyResourceBundle
				.getBundle(DB_CONFIG_FILE);

		Enumeration<String> enu = resourceBundle.getKeys();
		while (enu.hasMoreElements()) {
			String propertyName = enu.nextElement().toString();
			if (propertyName.equals(CONN_DRIVER_CLASS))
				driverClass = resourceBundle.getString(CONN_DRIVER_CLASS);
			if (propertyName.equals(CONN_URL))
				url = resourceBundle.getString(CONN_URL);
			if (propertyName.equals(CONN_USERNAME))
				username = resourceBundle.getString(CONN_USERNAME);
			if (propertyName.equals(CONN_PASSWORD))
				password = resourceBundle.getString(CONN_PASSWORD);
		}
	}

	/**
	 * 取得数据库连接
	 */
	public synchronized static Connection getConnection() {
		Connection conn = connection.get();

		//JDBC驱动连接
		try {
			if (conn != null && !conn.isClosed()) {
				return conn;
			}
			
			Class.forName(driverClass);
			conn = DriverManager.getConnection(url, username, password);
			conn.setAutoCommit(true);//默认自动提交,无事务
			connection.set(conn);
			
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			conn = null;
			connection.set(conn);
		}

		return conn;
	}

	public static void commit() {
		Connection conn = connection.get();
		try {
			conn.commit();
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
	}

	public static void rollback() {
		Connection conn = connection.get();
		try {
			conn.rollback();
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
	}

	public synchronized static void close() {
		Connection conn = connection.get();
		try {
			if(conn != null && !conn.isClosed())
				conn.close();
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		conn = null;
		connection.set(conn);
	}

	public static void main(String[] args) {

		try {
			System.out.println("begin try to connect...");
			Database.getConnection();
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		} finally {
			close();
		}
		
		System.out.println("finished!");
	}
}